<?php
/**
 * 注册页面
 * 州弟医院信息系统 - 州弟学安全
 */

// 包含头文件
include 'includes/header.php';

// 已登录用户重定向到首页
if (isLoggedIn()) {
    redirect('index.php');
}

// 处理注册表单提交
$error = '';
$success = '';
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
    $username = $_POST['username'] ?? '';
    $email = $_POST['email'] ?? '';
    $password = $_POST['password'] ?? '';
    $confirm_password = $_POST['confirm_password'] ?? '';
    $name = $_POST['name'] ?? '';
    $gender = $_POST['gender'] ?? '';
    $birthday = $_POST['birthday'] ?? '';
    $id_card = $_POST['id_card'] ?? '';
    $phone = $_POST['phone'] ?? '';
    $address = $_POST['address'] ?? '';
    
    // 基本验证
    if (empty($username) || empty($email) || empty($password) || empty($name) || 
        empty($gender) || empty($birthday) || empty($id_card) || empty($phone)) {
        $error = '请填写所有必填字段';
    } else if ($password != $confirm_password) {
        $error = '两次输入的密码不一致';
    } else if (!isValidEmail($email)) {
        $error = '请输入有效的电子邮件地址';
    } else if (!isValidPhone($phone)) {
        $error = '请输入有效的手机号码';
    } else if (!isValidIdCard($id_card)) {
        $error = '请输入有效的身份证号码';
    } else {
        // 检查用户名和邮箱是否已存在
        $username = $conn->real_escape_string($username);
        $email = $conn->real_escape_string($email);
        
        $sql = "SELECT * FROM users WHERE username = '$username' OR email = '$email'";
        $result = $conn->query($sql);
        
        if ($result && $result->num_rows > 0) {
            $error = '用户名或电子邮件已被注册';
        } else {
            // 创建新用户
            $password = md5($password); // 使用MD5加密密码 (注：实际应用中应使用更安全的密码哈希方法)
            $name = $conn->real_escape_string($name);
            $gender = $conn->real_escape_string($gender);
            $birthday = $conn->real_escape_string($birthday);
            $id_card = $conn->real_escape_string($id_card);
            $phone = $conn->real_escape_string($phone);
            $address = $conn->real_escape_string($address);
            
            // 开始事务
            $conn->begin_transaction();
            
            try {
                // 插入用户表
                $sql = "INSERT INTO users (username, password, email, role) VALUES ('$username', '$password', '$email', 'patient')";
                $conn->query($sql);
                
                $user_id = $conn->insert_id;
                
                // 插入患者表
                $sql = "INSERT INTO patients (user_id, name, gender, birthday, id_card, phone, address) 
                        VALUES ($user_id, '$name', '$gender', '$birthday', '$id_card', '$phone', '$address')";
                $conn->query($sql);
                
                // 记录注册日志
                logAction($user_id, '用户注册');
                
                // 提交事务
                $conn->commit();
                
                $success = '注册成功！请使用您的用户名和密码登录。';
            } catch (Exception $e) {
                // 回滚事务
                $conn->rollback();
                $error = '注册失败：' . $e->getMessage();
            }
        }
    }
}
?>

<div class="row justify-content-center">
    <div class="col-md-8">
        <div class="card">
            <div class="card-header">
                <h4 class="mb-0">用户注册</h4>
            </div>
            <div class="card-body">
                <?php if ($error): ?>
                    <div class="alert alert-danger"><?php echo $error; ?></div>
                <?php endif; ?>
                
                <?php if ($success): ?>
                    <div class="alert alert-success"><?php echo $success; ?></div>
                    <p class="text-center">
                        <a href="login.php" class="btn btn-primary">立即登录</a>
                    </p>
                <?php else: ?>
                    <form method="post" action="register.php" class="needs-validation" novalidate>
                        <div class="row">
                            <div class="col-md-6">
                                <h5 class="mb-3">账号信息</h5>
                                
                                <div class="form-group">
                                    <label for="username">用户名 <span class="text-danger">*</span></label>
                                    <input type="text" class="form-control" id="username" name="username" required>
                                    <small class="form-text text-muted">用户名将用于登录，不能修改</small>
                                </div>
                                
                                <div class="form-group">
                                    <label for="email">电子邮箱 <span class="text-danger">*</span></label>
                                    <input type="email" class="form-control" id="email" name="email" required>
                                </div>
                                
                                <div class="form-group">
                                    <label for="password">密码 <span class="text-danger">*</span></label>
                                    <input type="password" class="form-control" id="password" name="password" required>
                                    <div class="password-strength-container mt-2" style="display: none;">
                                        <div class="password-strength"></div>
                                        <small class="password-strength-text text-muted"></small>
                                    </div>
                                </div>
                                
                                <div class="form-group">
                                    <label for="confirm_password">确认密码 <span class="text-danger">*</span></label>
                                    <input type="password" class="form-control" id="confirm_password" name="confirm_password" required>
                                    <div class="invalid-feedback password-match-feedback">两次输入的密码不一致</div>
                                </div>
                            </div>
                            
                            <div class="col-md-6">
                                <h5 class="mb-3">个人信息</h5>
                                
                                <div class="form-group">
                                    <label for="name">姓名 <span class="text-danger">*</span></label>
                                    <input type="text" class="form-control" id="name" name="name" required>
                                </div>
                                
                                <div class="form-group">
                                    <label>性别 <span class="text-danger">*</span></label>
                                    <div>
                                        <div class="custom-control custom-radio custom-control-inline">
                                            <input type="radio" id="gender_male" name="gender" value="男" class="custom-control-input" required>
                                            <label class="custom-control-label" for="gender_male">男</label>
                                        </div>
                                        <div class="custom-control custom-radio custom-control-inline">
                                            <input type="radio" id="gender_female" name="gender" value="女" class="custom-control-input" required>
                                            <label class="custom-control-label" for="gender_female">女</label>
                                        </div>
                                    </div>
                                </div>
                                
                                <div class="form-group">
                                    <label for="birthday">出生日期 <span class="text-danger">*</span></label>
                                    <input type="date" class="form-control" id="birthday" name="birthday" required>
                                </div>
                                
                                <div class="form-group">
                                    <label for="id_card">身份证号 <span class="text-danger">*</span></label>
                                    <input type="text" class="form-control" id="id_card" name="id_card" required>
                                </div>
                                
                                <div class="form-group">
                                    <label for="phone">手机号码 <span class="text-danger">*</span></label>
                                    <input type="tel" class="form-control" id="phone" name="phone" required>
                                </div>
                                
                                <div class="form-group">
                                    <label for="address">住址</label>
                                    <textarea class="form-control" id="address" name="address" rows="2"></textarea>
                                </div>
                            </div>
                        </div>
                        
                        <div class="form-group form-check mt-3">
                            <input type="checkbox" class="form-check-input" id="agree_terms" required>
                            <label class="form-check-label" for="agree_terms">我已阅读并同意 <a href="#" data-toggle="modal" data-target="#termsModal">用户协议和隐私政策</a></label>
                        </div>
                        
                        <div class="text-center mt-4">
                            <button type="submit" class="btn btn-primary btn-lg">注册</button>
                        </div>
                    </form>
                <?php endif; ?>
            </div>
            <div class="card-footer text-center">
                <p class="mb-0">已有账号？ <a href="login.php">立即登录</a></p>
            </div>
        </div>
    </div>
</div>

<!-- 用户协议模态框 -->
<div class="modal fade" id="termsModal" tabindex="-1" role="dialog" aria-labelledby="termsModalLabel" aria-hidden="true">
    <div class="modal-dialog modal-lg" role="document">
        <div class="modal-content">
            <div class="modal-header">
                <h5 class="modal-title" id="termsModalLabel">用户协议和隐私政策</h5>
                <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                    <span aria-hidden="true">&times;</span>
                </button>
            </div>
            <div class="modal-body">
                <h5>用户协议</h5>
                <p>欢迎使用州弟医院管理系统。在您注册成为本系统用户前，请您仔细阅读以下条款...</p>
                <h5>隐私政策</h5>
                <p>我们非常重视您的个人信息和隐私保护。本隐私政策将向您说明我们如何收集、使用、存储和保护您的个人信息...</p>
            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-primary" data-dismiss="modal">我已阅读并同意</button>
            </div>
        </div>
    </div>
</div>

<?php
// 包含尾文件
include 'includes/footer.php';
?> 